package com.service;
import com.dao.AsRolePremissionDao;
import com.entity.AsRolePremission;
import com.entity.AsUser;
import com.utils.JsonUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;

@Service
public class AsRolePremissionService
{
    @Resource
    private AsRolePremissionDao asRolePremissionDao;

    //添加或更新角色授权
    public String doAddOrUpdatePremission( Integer roleId , Integer functionId , AsUser user )
    {
        Integer resultCode = 0;
        //首先查询as_role_premission表 查看是否存在该授权,存在则更新时间,不存在则写入表
        AsRolePremission arp = asRolePremissionDao.selectByRoleIdAndFunctionId( roleId , functionId );
        if( arp == null )
        {
            arp = new AsRolePremission();
            arp.setRoleId( roleId );
            arp.setFunctionId( functionId );
            arp.setCreationTime( new Date() );
            arp.setLastUpdateTime( new Date() );
            arp.setCreatedBy( user.getUserName() );
            arp.setIsStart( true );
            resultCode = asRolePremissionDao.addOneRolePremission( arp );
        }
        else
        {
            arp.setLastUpdateTime( new Date() );
            arp.setCreatedBy( user.getUserName() );
            arp.setIsStart(  true );
            resultCode = asRolePremissionDao.updateOneRolePremission( arp );
        }

        return JsonUtil.successOrNotJsonStr( resultCode );
    }

    //删除一条角色授权
    public String doCancelPremission( Integer roleId , Integer functionId )
    {
        AsRolePremission arp = new AsRolePremission();
        arp.setRoleId( roleId );
        arp.setFunctionId( functionId );
        Integer resultCode = asRolePremissionDao.delOneRolePremission( arp );
        return JsonUtil.successOrNotJsonStr( resultCode );
    }

}
